<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>


    <form action="/user-post" method="post">
        <input type="text" name="name">
        <input type="text" name="age">
        <input type="text" name="gender">
        <button>提交</button>
    </form>



    <script>
        const oForm = document.querySelector("form")
        const oName = document.querySelector("form input[name=name]")
        const oAge = document.querySelector("form input[name=age]")
        const oGender = document.querySelector("form input[name=gender]")
        const oBtn = document.querySelector("button")

        oBtn.onclick = function () {

            const nameValue = oName.value
            const ageValue = oAge.value
            const genderValue = oGender.value

            // 1.实例化XHMHttpRequest
            const xhr = new XMLHttpRequest()

            // 2.打开路径
            xhr.open("post", "http://127.0.0.1:8383/user-post", true)

            //3.发送请求
            xhr.setRequestHeader("content-Type", "application/x-www-form-urlencoded;charset=utf-8")
            xhr.send(`name=${nameValue}&age=${ageValue}&gender=${genderValue}`)

            //4.绑定监听
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 & xhr.status >= 200 & xhr.status <= 299) {
                    console.log(xhr.responseText);
                }
            }


            return false
        }
    </script>
</body>

</html>